package com.smalls.newvideotwo.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.bean.PosterList;
import com.smalls.newvideotwo.mvp.bean.PhotoInfo;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.view.GlideRoundTransform;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ImageLoaderUtil {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "ImageLoaderUtil":"ImageLoaderUtil";
    private int index = -1;

    public enum ImageType {
        Logo("logo"),
        Banner("banner"),
        Icon("icon"),
        Topic(Constant.TOPIC),
        Special_topic("special_topic"),
        Poster("poster"),
        Background("background");
        
        private String type;

        private ImageType(String type) {
            this.type = type;
        }

        public String getType() {
            return this.type;
        }
    }

    public void loadImageToView(ArrayList<PosterList> webmediaModels, ImageType type, ImageView imageView, Context context, int mDrawable, float radius) {
        if (webmediaModels != null && webmediaModels.size() > 0) {
            String path = getImagePath(webmediaModels, type);
            if (TextUtils.isEmpty(path)) {
                imageView.setImageResource(mDrawable);
            } else {
                loadImage(path, imageView, context, mDrawable, radius);
            }
        }
    }

    public void loadImageToView_ex(List<PhotoInfo> webmediaModels, ImageType type, ImageView imageView, Context context, int mDrawable, float radius) {
        if (webmediaModels != null && webmediaModels.size() > 0) {
            String path = getImagePath_ex(webmediaModels, type);
            if (TextUtils.isEmpty(path)) {
                imageView.setImageResource(mDrawable);
            } else {
                loadImage(path, imageView, context, mDrawable, radius);
            }
        }
    }

    public void loadImage(String path, ImageView imageView, Context context, final int mDrawable, float radius) {
        final String url = path;
        imageView.setTag(R.id.image_tag, url);
        Transformation[] transformationArr = new Transformation[]{new GlideRoundTransform(context, radius)};
        final ImageView imageView2 = imageView;
        final Context context2 = context;
        final int i = mDrawable;
        final String str = path;
        final float f = radius;
        Glide.with(context).load(url).crossFade().placeholder(mDrawable).error(mDrawable).diskCacheStrategy(DiskCacheStrategy.ALL).into(new SimpleTarget<GlideDrawable>() {
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                //LogUtil.d(ImageLoaderUtil.TAG, "onResourceReady loadImage url=" + url);
                if (url!=null&&url.equals(imageView2.getTag(R.id.image_tag))) {
                    imageView2.setImageDrawable(resource);
                } else if(url==null) {
                    imageView2.setBackgroundResource(mDrawable);
                }
            }

            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
                //LogUtil.d(ImageLoaderUtil.TAG, "onLoadFailed loadImage url=" + url);
                if (url!=null&&url.equals(imageView2.getTag(R.id.image_tag))) {
                    imageView2.setBackgroundResource(mDrawable);
                    //ImageLoaderUtil.this.loadImage(str, imageView2, context2, i, f);
                }else if(url==null) {
                    imageView2.setBackgroundResource(mDrawable);
                }
            }
        });
    }

    public static String getImagePath(ArrayList<PosterList> webmediaModels, ImageType type) {
        if (webmediaModels == null || webmediaModels.size() <= 0) {
            return null;
        }
        Iterator it = webmediaModels.iterator();
        while (it.hasNext()) {
            PosterList model = (PosterList) it.next();
            if (!TextUtils.isEmpty(model.getFileType()) && model.getFileType().equalsIgnoreCase(type.getType())) {
                return model.getFileUrl();
            }
        }
        return null;
    }

    public static String getImagePath_ex(List<PhotoInfo> webmediaModels, ImageType type) {
        if (webmediaModels == null || webmediaModels.size() <= 0) {
            return null;
        }
        Iterator it = webmediaModels.iterator();
        while (it.hasNext()) {
            PhotoInfo model = (PhotoInfo) it.next();
            if (!TextUtils.isEmpty(model.getMedia_type()) && model.getMedia_type().equalsIgnoreCase(type.getType())) {
                return model.getMedia_url();
            }
        }
        return null;
    }
}
